package com.sinven.controller.manage;

import com.sinven.common.ActionResult;
import com.sinven.pojo.Student;
import com.sinven.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;


    @GetMapping("students")
    public ActionResult<List<Student>> queryAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        return studentService.queryStudent(page, pageSize);
    }
    @GetMapping("students/{sid}")
    public ResponseEntity<Student> getStudent(@PathVariable("sid") long sid){
        Student student = studentService.queryStudent(sid);
        return ResponseEntity.status(HttpStatus.OK).body(student);
    }

    @PostMapping("students")
    public ResponseEntity<Void> addStudent(Student student){
        boolean result = studentService.addStudent(student);
        if(result){
           return ResponseEntity.status(HttpStatus.OK).body(null);
        }
        else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 修改就业信息
     *
     * @param student 就业信息
     * @return <i>true</i>修改成功<i>false</i>修改失败
     */
    @PutMapping("students")
    public ResponseEntity<Void> updateStudent(@RequestBody Student student) {
        log.debug(student.toString());
        boolean result =  studentService.updateStudent(student);
        if(result){
            return ResponseEntity.status(HttpStatus.OK).body(null);
        }
        else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @DeleteMapping("students/{sid}")
    public ResponseEntity<Void> deleteStudent(@PathVariable("sid") long sid) {
        boolean result =  studentService.deleteStudent(sid);
        if(result){
            return ResponseEntity.status(HttpStatus.OK).body(null);
        }
        else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}
